const util = {
  //日期格式化 yyyy-MM-dd hh:mm:ss
  formatDate(datetimeObj, fmt) {
    if (typeof datetimeObj === "string") {
      if (datetimeObj.length > 19) {
        datetimeObj = datetimeObj.substring(0, 19)
      }
      datetimeObj = datetimeObj.replace('T', ' ').replace(/-/g, '/'); //ios不支持yyyy-MM-dd
      var ms = Date.parse(datetimeObj);
      datetimeObj = new Date(ms);
    }
    let o = {
      "M+": datetimeObj.getMonth() + 1, //月份 
      "d+": datetimeObj.getDate(), //日 
      "h+": datetimeObj.getHours() % 12 == 0 ? 12 : datetimeObj.getHours() % 12, //12进制小时 
      "H+": datetimeObj.getHours(), //24进制小时 
      "m+": datetimeObj.getMinutes(), //分 
      "s+": datetimeObj.getSeconds(), //秒 
      "q+": Math.floor((datetimeObj.getMonth() + 3) / 3), //季度 
      "S": datetimeObj.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (datetimeObj.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  },
  //手机验证
  isMoile: (mobile) => {
    let re = /^1[3456789]\d{9}$/;
    return re.test(mobile);
  },
  //检测权限
  checkSetting: (scope) => {
    return new Promise(function (resolve, reject) {
      //查询权限
      wx.getSetting({
        success(res) {
          //如果存在授权
          if (res.authSetting[scope]) {
            console.log('13');
            resolve(res);
          } else {
            console.log('14');
            //进行授权
            wx.authorize({
              scope: scope,
              success(rt) {
                resolve(rt);
              },
              fail(rt) {
                reject(rt);
              }
            });
          }
        },
        fail(res) {
          reject(res);
        }
      });
    });
  },
  //是否为空
  isNullOrEmpty: function (str) {
    if (str) {
      str = str.toString().replace(/^\s+|\s+$/g, '');
      if (str == "null" || str == "") {
        return true;
      } else {
        return false;
      }
    } else {
      return true;
    }
  },
  //开启权限
  openSetting: (scope, content) => {
    return new Promise(function (resolve, reject) {
      wx.showModal({
        title: '提示',
        content: content,
        success(res) {
          if (res.confirm) {
            wx.openSetting({
              success: (rt) => {
                if (rt.authSetting[scope]) {
                  resolve(rt);
                } else {
                  reject(rt);
                }
              },
              fail: (rt) => {
                reject(rt);
              }
            })
          } else if (res.cancel) {
            reject(res);
          }
        }
      })
    });
  },
  /**
   * 根据对象中某一个属性的值进行排序
   * @param {* 对象属性值 String} attr 
   * @param {* 是否反转数组 Boolean} rev 
   */
  objCompare: function (attr, rev) {
    //第二个参数没有传递 默认升序排列
    if (rev == undefined) {
      rev = 1;
    } else {
      rev = (rev) ? 1 : -1;
    }

    return function (a, b) {
      a = a[attr];
      b = b[attr];
      if (a < b) {
        return rev * -1;
      }
      if (a > b) {
        return rev * 1;
      }
      return 0;
    }
  },
  /**
   * 计算两个日期相差的天数
   * start  开始日期  yyyy-MMM-dd
   * end   结束日期  yyyy-MMM-dd
   * return   day天数
   */
  countDateRange: function (start, end) {
    let diff = new Date(end).getTime() - new Date(start).getTime();
    diff = Math.abs(diff);
    let constantNum = 24 * 3600 * 1000; //毫秒数
    let day = Math.floor(diff / constantNum);
    return day;
  },
  /**
   * 输入时间返回该年返回天数
   * str  2019-01-02
   */
  getFullYearDayCount: function (str) {
    if (str) {
      let year = str.slice(0, 4);
      if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
        return 366;
      } else {
        return 365;
      }
    }
  },
  /**
   * guid
   */
  getGuid: function () {
    var ID = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      var r = Math.random() * 16 | 0,
        v = c == 'x' ? r : (r & 0x3 | 0x8);
      return v.toString(16);
    });
    return ID;
  }
};

module.exports = util